// MyProgress.cpp : implementation file
//

#include "stdafx.h"
#include "TOOLBOX4.h"
#include "MyProgress.h"


// CMyProgress

IMPLEMENT_DYNAMIC(CMyProgress, CProgressCtrl)

CMyProgress::CMyProgress()
{

}

CMyProgress::~CMyProgress()
{
}


void CMyProgress::UpdateMemDc(CDC* pDC, CRect& rt)
{
	if (!pDC) {
		// Handle the error case
		return;
	}
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(pDC, rt.Width(), rt.Height());
	if (bitmap.m_hObject == NULL || m_brush.m_hObject == NULL) {
		// Handle the error case
		return;
	}
	CDC dcMem;
	dcMem.CreateCompatibleDC(pDC);
	dcMem.SelectObject(&bitmap);
	dcMem.SelectObject(&m_brush);
	dcMem.PatBlt(0, 0, rt.Width(), rt.Height(), PATCOPY);
	int nMin, nMax, nCurPos;
	GetRange(nMin, nMax);
	nCurPos = GetPos();

	ASSERT(nMin <= nMax);
	ASSERT(nCurPos >= nMin && nCurPos <= nMax);

	int nDstWidth = (nMax != nMin) ? nCurPos * rt.Width() / (nMax  - nMin) : 0;
	nDstWidth = nDstWidth ? nDstWidth : 1;
	int nSrcWidth = min(nDstWidth, m_imgBar.GetWidth());
	m_imgBack.Draw(dcMem.GetSafeHdc(), 0, 0, rt.Width(), rt.Height());
	if(nCurPos > 0 && nCurPos != 100)
	{
		int n;
		if(nCurPos> 60 )
			n = 1;
		else if(nCurPos >= 30 && nCurPos <= 60)
			n = 2;
		else if(nCurPos >= 10 && nCurPos < 30)
			n = 3;
		else if(nCurPos < 10)
			n = 4;
		//m_imgBar.Draw(dcMem.GetSafeHdc(), 0, 0, nDstWidth, rt.Height());
		m_imgBar.Draw(dcMem.GetSafeHdc(), n, 0, nDstWidth, rt.Height());
	}
	else if(nCurPos == 100)
	{
		m_imgFull.Draw(dcMem.GetSafeHdc(), 0, 0, nDstWidth, rt.Height());
	}

	pDC->BitBlt(0, 0, rt.Width(), rt.Height(), &dcMem, 0, 0, SRCCOPY);
}

BEGIN_MESSAGE_MAP(CMyProgress, CProgressCtrl)
	ON_WM_CREATE()
	ON_WM_PAINT()
END_MESSAGE_MAP()



// CMyProgress message handlers




int CMyProgress::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CProgressCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  Add your specialized creation code here
	CString modPath = GetModulePath().c_str();
	modPath += _T("\\");
	if(!LoadImg(modPath+_T("res/progress_loading.png"), m_imgBar)){
		MessageBox(TEXT("Load resources failed!"), TEXT("ImageProgress error"), MB_ICONERROR);
		return -1;
	}

	if(!LoadImg(modPath+_T("res/progress_back.png"), m_imgBack)){
		MessageBox(TEXT("Load resources failed!"), TEXT("ImageProgress error"), MB_ICONERROR);
		return -1;
	}

	if(!LoadImg(modPath+_T("res/progress_full.png"), m_imgFull)){
		MessageBox(TEXT("Load resources failed!"), TEXT("ImageProgress error"), MB_ICONERROR);
		return -1;
	}

	m_BackWide = m_imgBack.GetWidth();
	m_backHight = m_imgBack.GetHeight();

	CImage mImg;	
	if(LoadImg(modPath+_T("res/background-new.png"), mImg))
	{
		COLORREF mcol = mImg.GetPixel(170, 420);
		//int r = GetRValue(mcol);
		//int g = GetGValue(mcol);
		//int b = GetBValue(mcol);
		m_brush.CreateSolidBrush(mcol);
	}

	return 0;
}


void CMyProgress::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CProgressCtrl::OnPaint() for painting messages

	CRect rt;
	GetWindowRect(rt);
	UpdateMemDc(&dc, rt);
}
